home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / NETUSER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  2.9 KB  |  157 lines

  1. /* Miscellaneous interger and IP address format conversion subroutines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "ctype.h"
  6. #include "netuser.h"
  7. #include "domain.h"
  8.  
  9. #if !defined(_lint)
  10. static char rcsid[] OPTIONAL = "$Id: netuser.c,v 1.12 1997/08/19 01:19:22 root Exp root $";
  11. #endif
  12.  
  13.  
  14. int Net_error;
  15.  
  16. extern const char *tcp_port_name (int portnum);
  17. long btol (char *s);
  18.  
  19.  
  20. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  21.  * binary IP address
  22.  */
  23. uint32
  24. aton (s)
  25. register const char *s;
  26. {
  27. uint32 n;
  28. register int i;
  29.  
  30.     n = 0;
  31.     if (s == NULLCHAR)
  32.         return 0;
  33.     for (i = 24; i >= 0; i -= 8) {
  34.         /* Skip any leading stuff (e.g., spaces, '[') */
  35.         while (*s != '\0' && !isdigit (*s))
  36.             s++;
  37.         if (*s == '\0')
  38.             break;
  39.         n |= (uint32) atoi (s) << i;
  40.         if ((s = strchr (s, '.')) == NULLCHAR)
  41.             break;
  42.         s++;
  43.     }
  44.     return n;
  45. }
  46.  
  47.  
  48. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  49.  * string, e.g., 255.255.255.255\0
  50.  */
  51. char *
  52. inet_ntoa (a)
  53. uint32 a;
  54. {
  55. static char buf[25];
  56. char *name;
  57.  
  58.     buf[0] = '\0';
  59.     if (DTranslate && (name = resolve_a (a, !DVerbose)) != NULLCHAR) {
  60.         strncpy (buf, name, 24);
  61.         buf[24] = '\0';
  62.         free (name);
  63.     } else    /*lint -save -e704 */
  64.         sprintf (buf, "%u.%u.%u.%u", hibyte (hiword (a)), lobyte (hiword (a)),
  65.              hibyte (loword (a)), lobyte (loword (a)));
  66.         /*lint -restore */
  67.     return buf;
  68. }
  69.  
  70.  
  71. /* Convert an internet address (in host byte order) to a unformated string
  72.  */
  73. char *
  74. inet_ntobos (a)
  75. uint32 a;
  76. {
  77. static unsigned char buf[5];
  78.  
  79.     /*lint -save -e704 */
  80.     buf[0] = hibyte (hiword (a));
  81.     buf[1] = lobyte (hiword (a));
  82.     buf[2] = hibyte (loword (a));
  83.     buf[3] = lobyte (loword (a));
  84.     /*lint -restore */
  85.     buf[4] = '\0';
  86.  
  87.     return (char *)buf;
  88. }
  89.  
  90.  
  91. /* Convert hex-ascii string to long integer */
  92. long
  93. htol (s)
  94. char *s;
  95. {
  96. long ret;
  97. char c;
  98.  
  99.     ret = 0;
  100.     while ((c = *s++) != '\0') {
  101.         c &= 0x7f;
  102.         if (c == 'x')
  103.             continue;    /* Ignore 'x', e.g., '0x' prefixes */
  104.         if (c >= '0' && c <= '9')
  105.             ret = ret * 16 + (c - '0');
  106.         else if (c >= 'a' && c <= 'f')
  107.             ret = ret * 16 + (10 + c - 'a');
  108.         else if (c >= 'A' && c <= 'F')
  109.             ret = ret * 16 + (10 + c - 'A');
  110.         else
  111.             break;
  112.     }
  113.     return ret;
  114. }
  115.  
  116.  
  117.  
  118. /* Convert binary string to long integer */
  119. long
  120. btol (s)
  121. char *s;
  122. {
  123. long ret;
  124. char c;
  125.  
  126.     ret = 0;
  127.     while ((c = *s++) != '\0') {
  128.         c &= 0x7f;
  129.         if (c == '%')
  130.             continue;    /* Ignore '%' prefixes */
  131.         if (c == '0' || c == '1')
  132.             ret = ret * 2 + (c - '0');
  133.         else
  134.             break;
  135.     }
  136.     return ret;
  137. }
  138.  
  139.  
  140. char *
  141. pinet (s)
  142. struct socket *s;
  143. {
  144. static char buf[80];
  145. char port[11];
  146. const char *ptr;
  147.  
  148.     ptr = tcp_port_name (s->port);
  149.     if (!strcmp (ptr, "0") && s->port)
  150.         sprintf (port, "%u", s->port);
  151.     else
  152.         sprintf (port, ptr);
  153.  
  154.     sprintf (buf, "%s:%s", inet_ntoa (s->address), port);
  155.     return buf;
  156. }
  157.